﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH;
using QLBH.Model.Controller;
using QLBH.DataModel;
using QLBH.DTO;
using Module.Category.Model;

namespace Module.Category
{
    public partial class DeptManagement : ViewBase
    {
        private static DeptManagement instance;

        public static DeptManagement Instance
        {
            get 
            { 
                if (instance==null)
                {
                    instance = new DeptManagement();
                }
                return DeptManagement.instance; 
            }
           
        }
        public DeptManagement()
        {
            InitializeComponent();
        }
        private void LoadDepts()
        {
            List<PhongBan> depts=DeptController.Instance.GetAll().ToList();

            List<DeptModel> result=new List<DeptModel>();
            foreach (PhongBanDTO pb in PhongBanAssembler.ToDTOs(depts).ToList())
            {
                DeptModel model=new DeptModel()
                {
                    Id=pb.Id,
                    TenPhong=pb.TenPhong,
                    GhiChu=pb.GhiChu
                };
                result.Add(model);
            }
            dataGridViewX1.DataSource=result;
        }

        private void DeptManagement_Load(object sender, EventArgs e)
        {
            LoadDepts();
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            LoadDepts();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            AddEditDeptDlg addDept = new AddEditDeptDlg();
            addDept.DialogType = eDialogType.DL_ADD;
            if (addDept.ShowDialog()==DialogResult.OK)
            {
                LoadDepts();
            }
            
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            EditDeptInfo();
        }
        private void EditDeptInfo()
        {
            try
            {
                AddEditDeptDlg editDeptInfo = new AddEditDeptDlg();
                editDeptInfo.DialogType = eDialogType.DL_EDIT;
                DeptModel selecteddept=dataGridViewX1.CurrentRow.DataBoundItem as DeptModel;
                PhongBan currentDept = DeptController.Instance.FindBy(dept => dept.Id == selecteddept.Id).FirstOrDefault();
                if (currentDept!=null)
                {
                    editDeptInfo.NewDept=currentDept.ToDTO();

                    if (editDeptInfo.ShowDialog() == DialogResult.OK)
                    {                        
                        LoadDepts();
                    }
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void dataGridViewX1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            EditDeptInfo();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            try
            {
                DeptModel selecteddept = dataGridViewX1.CurrentRow.DataBoundItem as DeptModel;
                PhongBan currentDept = DeptController.Instance.FindBy(dept => dept.Id == selecteddept.Id).FirstOrDefault();
                if (currentDept != null)
                {
                    DeptController.Instance.Delete(currentDept);
                    LoadDepts();
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }  
        }

        private void dataGridViewX1_DataSourceChanged(object sender, EventArgs e)
        {
            buttonEdit.Enabled = dataGridViewX1.RowCount > 0;
            buttonDelete.Enabled = dataGridViewX1.RowCount > 0;
        }
    }
}
